package controller

import (
	"Bluebell/common/resp"
	"Bluebell/common/util"
	"Bluebell/logic"
	"Bluebell/models"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"go.uber.org/zap"
	"strconv"
)

func CreatePostHandler(context *gin.Context) {
	p := new(models.Post)
	if err := context.ShouldBindJSON(p); err != nil {
		resp.ResponseError(context, resp.CodeInvalidParams)
		return
	}

	//userID, err := GetCurrentUser(context)
	//if err != nil {
	//	resp.ResponseError(context, resp.CodeNotLogin)
	//	return
	//}
	p.AuthorId = uint64(137018984036827137)
	if err := logic.CreatePost(p); err != nil {
		zap.L().Error("logic.CreatePost err:", zap.Error(err))
		resp.ResponseError(context, resp.CodeServerBusy)
		return
	}
	resp.ResponseSuccess(context, nil)
}

func GetPostDetailHandler(context *gin.Context) {
	idStr := context.Param("id")
	id, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		resp.ResponseError(context, resp.CodeInvalidParams)
		return
	}
	data, err := logic.GetPostDetail(id)
	if err != nil {
		zap.L().Error("logic.GetPostDetail failed", zap.Error(err))
		resp.ResponseError(context, resp.CodeServerBusy)
		return
	}
	resp.ResponseSuccess(context, data)
}

func GetPostListHandler(context *gin.Context) {
	page, size := GetPageInfo(context)
	data, err := logic.GetPostList(page, size)
	if err != nil {
		zap.L().Error("logic.GetPostList failed:", zap.Error(err))
		resp.ResponseError(context, resp.CodeServerBusy)
		return
	}
	resp.ResponseSuccess(context, data)
}

func GetPageInfo(context *gin.Context) (int64, int64) {
	pageStr := context.Query("page")
	sizeStr := context.Query("size")
	var (
		page int64
		size int64
		err  error
	)
	page, err = strconv.ParseInt(pageStr, 10, 64)
	if err != nil {
		page = 0
	}
	size, err = strconv.ParseInt(sizeStr, 10, 64)
	if err != nil {
		size = 10
	}
	return page, size
}

func PostVoteHandler(context *gin.Context) {
	p := new(models.ParamVoteData)
	if err := context.ShouldBindJSON(p); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			resp.ResponseError(context, resp.CodeInvalidParams)
			return
		}
		_ = errs.Translate(util.Trans)
		resp.ResponseErrorWithMsg(context, resp.CodeInvalidParams, "CodeInvalidParams")
		return
	}

	//userID, err := GetCurrentUser(context)
	//if err != nil {
	//	resp.ResponseError(context, resp.CodeNotLogin)
	//	return
	//}
	userID := int64(264758726051762176)
	if err := logic.VoteForPost(userID, p); err != nil {
		zap.L().Error("logic.VoteForPost failed:", zap.Error(err))
		resp.ResponseError(context, resp.CodeServerBusy)
		return
	}
	resp.ResponseSuccess(context, nil)
}

// GetPostListHandler2 升级版帖子列表接口
// @Summary 升级版帖子列表接口
// @Description 可按社区按时间或分数排序查询帖子列表接口
// @Tags 帖子相关接口
// @Accept application/json
// @Produce application/json
// @Param Authorization header string false "Bearer 用户令牌"
// @Param object query models.ParamsPostList false "查询参数"
// @Security ApiKeyAuth
// @Success 200 {object} _ResponsePostList
// @Router /posts2 [get]
//根据分数或者时间排序列表
func GetPostListHandler2(context *gin.Context) {
	p := &models.ParamsPostList{
		Page:  1,
		Size:  10,
		Order: models.OrderTime,
	}
	if err := context.ShouldBindQuery(p); err != nil {
		zap.L().Error("context.ShouldBindQuery failed", zap.Error(err))
		resp.ResponseError(context, resp.CodeInvalidParams)
		return
	}
	data, err := logic.GetPostListNew(p)
	if err != nil {
		zap.L().Error("logic.GetPostList failed:", zap.Error(err))
		resp.ResponseError(context, resp.CodeServerBusy)
		return
	}
	resp.ResponseSuccess(context, data)

}
